package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetTopicNoticeListBean;
import com.rjwh.dingdong.client.bean.localbean.TopicNotice;
import com.rjwh.dingdong.client.bean.localbean.TopicNoticeListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityPostMsgModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPostMsgPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostMsgView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName CommunityPostMsgPresenter
 * @Description
 * @date 2016-6-30
 * @history 2016-6-30 author: description:
 */
public class CommunityPostMsgPresenter extends BasePresenter<CommunityPostMsgView> implements ICommunityPostMsgPresenter {
    private CommunityPostMsgModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public CommunityPostMsgPresenter(Context context, CommunityPostMsgView mMvpView) {
        super(context, mMvpView);
        model = new CommunityPostMsgModel(this);
    }


    /**
     * 获取帖子消息
     *
     * @param pageNumber 页码
     * @param isRefresh  是否刷新
     * @param direction
     */
    @Override
    public void doGetTopicNotice(int pageNumber, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetTopicNotice(pageNumber, isRefresh);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.COMMUNITY_GETNOTICELISTBYUSERID:
                    getMvpView().setRefreshing(false);
                    ResGetTopicNoticeListBean getTopicNoticeListBean = (ResGetTopicNoticeListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && getTopicNoticeListBean != null) {
                        TopicNoticeListData noticeListData = getTopicNoticeListBean.getData();
                        if (noticeListData != null) {
                            List<TopicNotice> noticelist = noticeListData.getNoticelist();
                            if (noticelist != null) {
                                if (noticelist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_msg));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, noticelist);
                            }

                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
